﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using BLL;
public partial class QuarterPlan_QuarterPlanMeetAdd : System.Web.UI.Page
{

    DataStructure dalDataStructure = new DataStructure();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    MeetRecord dllMeetRecord = new MeetRecord();

    Authentication dllAuthentication = new Authentication();
    string path = HttpContext.Current.Server.MapPath("~/") + @"Upload\";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDropDownList(this.drpYear);
            bindDropDownList(this.drpQuarterDate);
            binddata();
        }
    }
    void binddata()
    {
        string sid = GetID();
        if (sid != "-1")
        {
            object[] _value = new object[2];
            _value[0] = Convert.ToInt32(sid);
            _value[1] = Utility.HttpHelp.getParamsString("department");
            DataTable table = dllMeetRecord.getByID(_value);

            if (table.Rows.Count != 0)
            {

                string dpid = table.Rows[0]["QuarterDate"].ToString();
                drpQuarterDate.SelectedValue = dpid;
                txtMeetTime.Text = Convert.ToDateTime(table.Rows[0]["meettime"].ToString()).ToShortDateString();
                txtMeetRecord.Text = table.Rows[0]["Meetcontent"].ToString();
                if (table.Rows[0]["MeetPath"].ToString().Length > 0)
                {
                    string _fileName = table.Rows[0]["MeetPath"].ToString();
                    this.lblAttachmentPath.Visible = true;
                    this.lblAttachmentPath.Text = "附件名称为:" + _fileName;
                    this.fupFujian.Visible = false;
                    this.btnDisplay.Visible = true;
                }



            }
        }
    }

    string GetID()
    {
        string sid = "-1";
        if (Request.Params["id"] != null && Request.Params["id"].ToString().Length != 0)
        {
            sid = Request.Params["id"].ToString();
        }
        return sid;
    }

    /// <summary>
    /// 绑定DropDownList
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    void bindDropDownList(DropDownList _drp)
    {
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(li);
            }
            //根据系统日期自动绑定到当前年份
            //string _year = DateTime.Now.Year.ToString();
            //this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
            //根据系统当前日期查找到对应的年周期
            DataTable _year = dllQuarterDateManager.getQuarterDate();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));

        }
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                #region dropdownlist显示系统所对应的当前年周期
                //根据系统当前日期查找到对应的年周期
                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                //如果查到结果（结果只有一条）

                if (_quartertime.Rows.Count == 1)
                {
                    this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                    this.drpQuarterDate.DataSource = _quartertime;
                    this.drpQuarterDate.DataTextField = "QuarterName";
                    this.drpQuarterDate.DataValueField = "Quarter_Id";
                }
                #endregion

            }
            else
            {
                ListItem li = new ListItem("无年周期");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(li);

            }

        }
    }


    /// <summary>
    /// 添加年会议记录
    /// </summary>
    void addQuarterMeetRecord()
    {
        if (this.fupFujian.FileName.Length > 0)
        {
            //获得附件文件名
            string _filename = this.fupFujian.FileName;
            //判断是否存在
            if (System.IO.File.Exists(path + _filename))
            {
                System.IO.File.Delete(path + _filename);
            }
            else
            {
                //弹出对话框报已存在！
            }
            this.fupFujian.SaveAs(path + _filename);
            //构造数组
            object[] _value = new object[5];
            _value[0] = Convert.ToInt32(this.drpQuarterDate.SelectedItem.Value);
            _value[1] = Convert.ToDateTime(txtMeetTime.Text);
            _value[2] = txtMeetRecord.Text;
            _value[3] = _filename;
            _value[4] = Utility.HttpHelp.getParamsString("department");
            int result = dllMeetRecord.add(_value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加会议记录未成功，请重试！");
            }
        }
        else
        {
            //构造数组
            object[] _value = new object[5];
            _value[0] = Convert.ToInt32(this.drpQuarterDate.SelectedItem.Value);
            _value[1] = Convert.ToDateTime(txtMeetTime.Text);
            _value[2] = txtMeetRecord.Text;
            _value[3] = "";
            _value[4] = Utility.HttpHelp.getParamsString("department");
            int result = dllMeetRecord.add(_value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加会议记录未成功，请重试！");
            }
        }
    }


    /// <summary>
    /// 修改年会议记录
    /// </summary>
    void updateQuarterMeetRecord()
    {
        if (this.fupFujian.FileName.Length > 0)
        {
            //获得附件文件名
            string _filename = this.fupFujian.FileName;
            //判断是否存在
            if (System.IO.File.Exists(path + _filename))
            {
                System.IO.File.Delete(path + _filename);
            }
            else
            {
                //弹出对话框报已存在！
            }
            this.fupFujian.SaveAs(path + _filename);
            //构造数组
            object[] _value = new object[4];
            _value[0] = this.txtMeetRecord.Text;
            _value[1] = _filename;// path + _filename;
            _value[2] = Convert.ToDateTime(this.txtMeetTime.Text);
            _value[3] = Utility.HttpHelp.getParamsString("department");
            int result = dllMeetRecord.edit(MeetRecord.field.MeetTime.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("自动保存会议记录未成功，请重试！");
            }
        }
        else
        {
            //构造数组
            object[] _value = new object[4];
            _value[0] = this.txtMeetRecord.Text;
            _value[1] = "";
            _value[2] = Convert.ToDateTime(this.txtMeetTime.Text);
            _value[3] = Utility.HttpHelp.getParamsString("department");
            int result = dllMeetRecord.edit(MeetRecord.field.MeetTime.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("自动保存会议记录未成功，请重试！");
            }
        }

    }


    /// <summary>
    /// 提交方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btntijiao_Click(object sender, EventArgs e)
    {
        //判断是否存在
        object[] _value = new object[2];
        _value[0] = Convert.ToDateTime(this.txtMeetTime.Text);
        _value[1] = Utility.HttpHelp.getParamsString("department");
        DataTable _meetRecord = dllMeetRecord.get(MeetRecord.field.MeetTime.ToString(), _value);
        if (_meetRecord.Rows.Count > 0)
        {
            updateQuarterMeetRecord();
        }
        else
        {
            addQuarterMeetRecord();
        }
        Response.Redirect("QuarterPlanMeet.aspx");
    }


    /// <summary>
    /// Timer自动保存事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (this.txtMeetTime.Text.Length > 0)
        {
            //判断是否存在
            object[] _value = new object[2];

            _value[0] = Convert.ToDateTime(this.txtMeetTime.Text);
            _value[1] = Utility.HttpHelp.getParamsString("department");
            DataTable _meetRecord = dllMeetRecord.get(MonthMeetRecord.field.MeetTime.ToString(), _value);
            if (_meetRecord.Rows.Count > 0)
            {
                updateQuarterMeetRecord();
            }
            else
            {
                addQuarterMeetRecord();
            }
        }
    }


    /// <summary>
    /// 重置事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnchongzhi_Click(object sender, EventArgs e)
    {
        Utility.HttpHelp.redirectPage("QuarterPlanMeet.aspx");
    }

    /// <summary>
    /// 年份改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
    }


    /// <summary>
    /// 重新上传事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        this.lblAttachmentPath.Visible = false;
        this.fupFujian.Visible = true;
        this.btnDisplay.Visible = false;
    }
}
